package com.new4net.exe_stream.listener.impl;

import com.new4net.exe_stream.listener.InstructionListener;
import com.new4net.exe_stream.meta.ExecutionContext;
import com.new4net.exe_stream.service.ExecutionLog;
import com.new4net.exe_stream.service.ExecutionLogService;
import com.new4net.exe_stream.instruction.Instruction;

public class ExecutionLogListener implements InstructionListener {
    @Override
    public String getName() {
        return "执行日志监听器";
    }

    @Override
    public void prev(Instruction instruction) {
        ExecutionContext executionContext = instruction.getExecutionContext();

        ExecutionLogService executionLogService = executionContext.getExecutionLogService();
        executionLogService.record(ExecutionLog
                .builder()
                .executionId(executionContext.getParam("executionId"))
                .executionName(executionContext.getParam("executionName"))
                .instructionId(instruction.getInstructionId())
                .instructionName(instruction.getInstructionName())
                .type("IN")
                .build());

    }

    @Override
    public void post(Instruction instruction) {
        ExecutionContext executionContext = instruction.getExecutionContext();

        ExecutionLogService executionLogService = executionContext.getExecutionLogService();
        executionLogService.record(ExecutionLog
                .builder()
                .executionId(executionContext.getParam("executionId"))
                .executionName(executionContext.getParam("executionName"))
                .instructionId(instruction.getInstructionId())
                .instructionName(instruction.getInstructionName())
                .type("OUT")
                .build());
    }

    @Override
    public void step(Instruction instruction, String stepName) {
        ExecutionContext executionContext = instruction.getExecutionContext();

        ExecutionLogService executionLogService = executionContext.getExecutionLogService();
        executionLogService.record(ExecutionLog
                .builder()
                .executionId(executionContext.getParam("executionId"))
                .executionName(executionContext.getParam("executionName"))
                .instructionId(instruction.getInstructionId())
                .instructionName(instruction.getInstructionName())
                .stepName(stepName)
                .type("IN")
                .build());
    }

    @Override
    public void fault(Throwable e, Instruction instruction) {
        ExecutionContext executionContext = instruction.getExecutionContext();

        ExecutionLogService executionLogService = executionContext.getExecutionLogService();
        executionLogService.record(ExecutionLog
                .builder()
                .executionId(executionContext.getParam("executionId"))
                .executionName(executionContext.getParam("executionName"))
                .instructionId(instruction.getInstructionId())
                .instructionName(instruction.getInstructionName())
                .type("ERROR")
                .error(e)
                .build());
    }
}
